<template>
  <view class="tree-container">
    <view 
      v-for="(item, index) in formattedTreeData" 
      :key="item[keyProp]" 
      class="tree-node"
    >
      <!-- 节点内容 -->
      <view class="node-content" @click="toggleExpand(item)">
        <!-- 展开/折叠图标 -->
        <view class="node-icon" v-if="item.children && item.children.length">
          <uni-icons 
            :type="item.expanded ? 'down' : 'arrowright'" 
            size="16" 
            color="#666"
          />
        </view>
        <view class="node-icon placeholder" v-else></view>
        
        <!-- 复选框 -->
        <checkbox 
          v-if="showCheckbox" 
          :value="item[keyProp]" 
          :checked="item.checked" 
          @click.stop="handleCheck(item)"
          style="margin-right: 8px;"
        />
        
        <!-- 节点文本 -->
        <text class="node-label" @click.stop="handleNodeClick(item)">
          {{ item[labelProp] }}
        </text>
      </view>
      
      <!-- 子节点 -->
      <view class="node-children" v-show="item.expanded" v-if="item.children && item.children.length">
        <tree 
          :data="item.children" 
          :labelProp="labelProp" 
          :keyProp="keyProp" 
          :showCheckbox="showCheckbox"
          :defaultExpandAll="defaultExpandAll"
          @node-click="handleChildNodeClick"
          @check-change="handleChildCheckChange"
        />
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, watch, reactive } from 'vue'
import tree from '@/packageA/components/tree/tree.vue'
const props = defineProps({
  // 树形数据
  data: {
    type: Array,
    default: () => []
  },
  // 显示复选框
  showCheckbox: {
    type: Boolean,
    default: false
  },
  // 默认展开所有节点
  defaultExpandAll: {
    type: Boolean,
    default: false
  },
  // 指定节点标签为节点对象的某个属性值
  labelProp: {
    type: String,
    default: 'label'
  },
  // 指定节点键值为节点对象的某个属性值
  keyProp: {
    type: String,
    default: 'id'
  }
})

const emit = defineEmits(['node-click', 'check-change'])

// 格式化后的树数据
const formattedTreeData = computed(() => {
  return formatTreeData(props.data, 0)
})

// 格式化树数据，添加层级和展开状态
const formatTreeData = (data, level, parent = null) => {
  return data.map(item => {
    const node = reactive({
      ...item,
      level: level,
      expanded: props.defaultExpandAll,
      checked: item.checked || false,
      parent: parent
    })
    
    if (item.children && item.children.length) {
      node.children = formatTreeData(item.children, level + 1, node)
    }
    
    return node
  })
}

// 切换节点展开状态
const toggleExpand = (node) => {
  if (node.children && node.children.length) {
    node.expanded = !node.expanded
    console.log(node.expanded,'切换');
    
  }
}

// 节点点击事件
const handleNodeClick = (node) => {
  emit('node-click', node)
}

// 子节点点击事件
const handleChildNodeClick = (node) => {
  emit('node-click', node)
}

// 复选框变化
const handleCheck = (node) => {
  node.checked = !node.checked
  emit('check-change', node)
  
  // 处理子节点
  if (node.children && node.children.length) {
    toggleCheckChildren(node.children, node.checked)
  }
  
  // 处理父节点
  if (node.parent) {
    checkParent(node.parent)
  }
}

// 子节点复选框变化
const handleChildCheckChange = (node) => {
  emit('check-change', node)
}

// 切换子节点选中状态
const toggleCheckChildren = (children, checked) => {
  children.forEach(child => {
    child.checked = checked
    if (child.children && child.children.length) {
      toggleCheckChildren(child.children, checked)
    }
  })
}

// 检查父节点状态
const checkParent = (parent) => {
  const allChildrenChecked = parent.children.every(child => child.checked)
  const someChildrenChecked = parent.children.some(child => child.checked)
  
  if (allChildrenChecked) {
    parent.checked = true
  } else if (someChildrenChecked) {
    parent.checked = 'indeterminate' // 半选状态，需要额外处理
  } else {
    parent.checked = false
  }
  
  if (parent.parent) {
    checkParent(parent.parent)
  }
}

// 暴露方法给父组件
defineExpose({
  getCheckedNodes: () => {
    const checkedNodes = []
    findCheckedNodes(formattedTreeData.value, checkedNodes)
    return checkedNodes
  }
})

// 递归查找选中的节点
const findCheckedNodes = (nodes, checkedNodes) => {
  nodes.forEach(node => {
    if (node.checked) {
      checkedNodes.push(node)
    }
    if (node.children && node.children.length) {
      findCheckedNodes(node.children, checkedNodes)
    }
  })
}
</script>

<style scoped>
.tree-container {
  font-size: 14px;
  color: #333;
}

.tree-node {
  padding: 8px 0;
}

.node-content {
  display: flex;
  align-items: center;
  padding: 4px 0;
}

.node-icon {
  width: 16px;
  height: 16px;
  margin-right: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.node-icon.placeholder {
  visibility: hidden;
}

.node-label {
  flex: 1;
}

.node-children {
  transition: all 0.3s;
  padding-left: 20px;
}
</style>